package com.efounder.utils;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.TextView;

import com.efounder.view.CustomDialog;

/**
 * 应用升级框
 *
 * @author lch
 */
public class ProgressDialogForUpdate {
    private Context context;
    private String dialogTitle;
    private String dialogMessage;
    private String positiveText;
    private String negativeText;
    private CustomDialog.Builder dialogBuild;
    private OnDialogListener listener;
    private boolean isShowing = false;

    /**
     * @param dialogTitle     对话框的标题
     * @param dialogMessage   对话框的内容
     * @param positiveText    表示是确定意图的按钮上text文本
     * @param negativeText    表示是取消意图的按钮上text文本
     * @param context         上下文对象
     * @自定义封装的通用对话框:ProgressDialogForUpdate 这个是参考网络上写的，为啥不直接用view呢？看到后面的build模式，就没有做修改。
     */
    public ProgressDialogForUpdate(Context context, String dialogMessage, String dialogTitle, String positiveText, String negativeText) {
        this.context = context;
        this.dialogTitle = dialogTitle;
        this.dialogMessage = dialogMessage;
        this.positiveText = positiveText;
        this.negativeText = negativeText;
    }

    /**
     * 初始化样式，并显示加载框
     */
    public void showDialog() {
        if (((Activity) context) == null || ((Activity) context).isFinishing()) {
            return;
        }
        dialogBuild = new CustomDialog.Builder(context);
        dialogBuild.setCancelable(true);
        dialogBuild.setCanceledOnTouchOutside(false);
        dialogBuild.setTitle(dialogTitle);//设置标题
        //注意:dialogMessage和dialogView是互斥关系也就是dialogMessage存在dialogView就不存在,dialogView不存在dialogMessage就存在
        if (dialogMessage != null) {
            dialogBuild.setMessage(dialogMessage);//设置对话框内容
        }

        dialogBuild.setPositiveButton(positiveText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                //dialogInterface.dismiss();
                if (listener != null) {
                    listener.dialogPositiveListener(dialogInterface, which);
                }
            }
        }).setNegativeButton(negativeText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                //dialogInterface.dismiss();
                if (listener != null) {
                    isShowing = false;
                    listener.dialogNegativeListener(dialogInterface, which);
                }
            }
        }).create();
        dialogBuild.getDialog().show();
        isShowing = true;
        dialogBuild.getDialog().setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                if (listener != null) {
                    isShowing = false;
                    listener.dialogCancelListener();
                }
            }
        });

    }

    public void dismiss() {
        if (((Activity) context) == null || ((Activity) context).isFinishing()) {
            return;
        }
        getDialog().dismiss();
    }

    /**
     * 判断是否是在显示
     *
     * @return
     */
    public boolean isShowing() {
        return isShowing;
    }

    /**
     * 得到dialog的view
     *
     * @return
     */
    public CustomDialog getDialog() {
        return dialogBuild.getDialog();
    }

    /**
     * 得到下载按钮
     *
     * @return
     */
    public TextView getPositiveView() {
        return dialogBuild.getPositiveButton();
    }

    /**
     * 得到取消按钮
     *
     * @return
     */
    public TextView getNegativeView() {
        return dialogBuild.getNegativeButton();

    }

    /**
     * 得到DialogBuild
     *
     * @return
     */
    public CustomDialog.Builder getDialogBuild() {
        return dialogBuild;

    }

    /**
     * 更新进度条
     *
     * @param progress
     */
    public void updateProgress(int progress) {
        dialogBuild.setProgress(progress);
    }

    //注册监听器方法
    public ProgressDialogForUpdate setOnDiaLogListener(OnDialogListener listener) {
        this.listener = listener;
        return this;//把当前对象返回,用于链式编程
    }

    //定义一个监听器接口
    public interface OnDialogListener {
        public void dialogPositiveListener(DialogInterface dialogInterface, int which);

        public void dialogNegativeListener(DialogInterface dialogInterface, int which);

        public void dialogCancelListener();
    }
}


